package cn.com.gridinfo.sooc.core.course.classes.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toolbar;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.course.classes.adapter.CourseKindsAdapter;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseKinds;
import cn.com.gridinfo.sooc.core.course.common.CodeConst;
import cn.com.gridinfo.sooc.core.course.classes.dao.CourseDao;
import cn.com.gridinfo.sooc.utils.widget.VideoEnabledWebChromeClient;
import cn.com.gridinfo.sooc.utils.widget.VideoEnabledWebView;

/**
 * 展示 webView 页面
 *
 * @author  jie.li
 */
public class CourseWebViewActivity extends BaseSoocActivity {

    @InjectView(R.id.sooc_course_webview)
    VideoEnabledWebView sooc_webview;//显示授课网页View控件
    @InjectView(R.id.webview_layout)
    RelativeLayout webviewLayout;//webview layout
    @InjectView(R.id.videoLayout)
    RelativeLayout videoLayout; //视频全屏layout

    String loadUrl = "";



    private VideoEnabledWebChromeClient webChromeClient;//播放网页视频会用到



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_show_web);
        ButterKnife.inject(this);
        Log.d("============点击首页推荐进入 web 浏览页面=======");
        Intent i = getIntent();
        loadUrl = i.getStringExtra("loadUrl");
        initWebView();
    }
    //点击返回
    @OnClick(R.id.toolbar_leftbtn)
    void goBack(){
        finish();
        AnimUtil.intentSlidOut(this);
    }

    //初始化 webView 控件
    private void initWebView() {
        //视频加载初始化界面
        View loadingView =  this.getLayoutInflater().inflate(R.layout.view_loading_video, null);

        //设置webview网页显示，加载url
        //webview 内网页跳转
        sooc_webview.setWebViewClient(new WebViewClient() {



            //当前加载
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i("sooc网链接接 : "+url);


                //排除一定位数的数字会自动成为电话号码
                if (url.indexOf("tel:") < 0) {
                    view.loadUrl(url);
                }

                return false;
            }

        });

//        加载 video 视频 client
        webChromeClient = new VideoEnabledWebChromeClient(this, webviewLayout, videoLayout, loadingView, sooc_webview) {
            @Override
            public void onProgressChanged(WebView view, int progress) {
            }

        };
        //设置 video 全屏和隐藏
        webChromeClient.onShowCustomView(sooc_webview, new VideoEnabledWebChromeClient.CustomViewCallback() {

            @Override
            public void onCustomViewHidden() {
                webChromeClient.onHideCustomView();
            }
        });

        sooc_webview.setClickable(true);
        sooc_webview.getSettings().setJavaScriptEnabled(true);//允许脚本
//        sooc_webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        sooc_webview.getSettings().setLoadWithOverviewMode(true);//加载页面的到屏幕大小模式
//        sooc_webview.getSettings().setSupportZoom(true);//不支持变焦
//        sooc_webview.getSettings().setBuiltInZoomControls(false);//不支持缩放
//        sooc_webview.getSettings().setDisplayZoomControls(false);
//
        sooc_webview.setWebChromeClient(webChromeClient);
        //设置字符编码
//        sooc_webview.getSettings().setDefaultTextEncodingName("UTF-8");


        sooc_webview.loadUrl(loadUrl);
    }

    //    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "推荐";
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        AnimUtil.intentSlidOut(this);
    }
}
